from reportlab.pdfgen import canvas
from reportlab.platypus import (SimpleDocTemplate, Paragraph, PageBreak, Image, Spacer, Table, TableStyle)
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet
from reportlab.lib.pagesizes import LETTER, inch
from reportlab.graphics.shapes import Line, LineShape, Drawing
from reportlab.lib.colors import Color

class FooterCanvas(canvas.Canvas):

    def __init__(self, *args, **kwargs):
        canvas.Canvas.__init__(self, *args, **kwargs)
        self.pages = []
        self.width, self.height = LETTER

    def showPage(self):
        self.pages.append(dict(self.__dict__))
        self._startPage()

    def save(self):
        page_count = len(self.pages)
        for page in self.pages:
            self.__dict__.update(page)
            if (self._pageNumber > 1):
                self.draw_canvas(page_count)
            canvas.Canvas.showPage(self)
        canvas.Canvas.save(self)

    def draw_canvas(self, page_count):
        page = "Page %s of %s" % (self._pageNumber, page_count)
        x = 128
        self.saveState()
        self.setStrokeColorRGB(0, 0, 0)
        self.setLineWidth(0.5)
        # self.drawImage("static/lr.png", self.width-inch*8-5, self.height-50, width=100, height=20, preserveAspectRatio=True)
        # self.drawImage("static/ohka.png", self.width - inch * 2, self.height-50, width=100, height=30, preserveAspectRatio=True, mask='auto')
        self.line(30, 740, LETTER[0] - 50, 740)
        self.line(66, 78, LETTER[0] - 66, 78)
        self.setFont('Times-Roman', 10)
        self.drawString(LETTER[0]-x, 65, page)
        self.restoreState()

class PDFPSReporte:

    def __init__(self, path,data_obj,graph,dataitems,graph_degra,cycles):
        self.path = path
        self.styleSheet = getSampleStyleSheet()
        self.elements = []
        self.data_obj=data_obj
        self.graph=graph
        self.dataitems=dataitems
        self.graph_degra=graph_degra
        self.cycles=cycles

        # colors - Azul turkeza 367AB3
        self.colorOhkaGreen0 = Color((45.0/255), (166.0/255), (153.0/255), 1)
        self.colorOhkaGreen1 = Color((182.0/255), (227.0/255), (166.0/255), 1)
        self.colorOhkaGreen2 = Color((140.0/255), (222.0/255), (192.0/255), 1)
        #self.colorOhkaGreen2 = Color((140.0/255), (222.0/255), (192.0/255), 1)
        self.colorOhkaBlue0 = Color((54.0/255), (122.0/255), (179.0/255), 1)
        self.colorOhkaBlue1 = Color((122.0/255), (180.0/255), (225.0/255), 1)
        self.colorOhkaGreenLineas = Color((50.0/255), (140.0/255), (140.0/255), 1)

        self.firstPage()
        self.nextPagesHeader(True)
        self.remoteSessionTableMaker()
        self.nextPagesHeader(False)
        self.xxSessionTableMaker()
        self.nextPagesHeader(False)
        self.inSiteSessionTableMaker()
        self.nextPagesHeader(False)
        # self.extraActivitiesTableMaker()
        self.nextPagesHeader(False)
        self.summaryTableMaker()
        # Build
        self.doc = SimpleDocTemplate(path, pagesize=LETTER)
        self.doc.multiBuild(self.elements, canvasmaker=FooterCanvas)

    def firstPage(self):
        # img = Image('static/lr.png', kind='proportional')
        # img.drawHeight = 0.5*inch
        # img.drawWidth = 2.4*inch
        # img.hAlign = 'LEFT'
        # self.elements.append(img)

        spacer = Spacer(30, 100)
        self.elements.append(spacer)

        img = Image('static/Reports.jpg')
        img.drawHeight = 2.5*inch
        img.drawWidth = 6.5*inch
        self.elements.append(img)

        spacer = Spacer(10, 100)
        self.elements.append(spacer)
        import time;date = time.strftime('%Y-%m-%d', time.localtime()) #print (ts)
        ts=time.strftime('%H:%M:%S', time.localtime())

        psDetalle = ParagraphStyle('Resumen', fontSize=24, leading=30, justifyBreaks=1, alignment=TA_LEFT, justifyLastLine=1)
        text = """Model consistency check report <br/>
        """

        infor=ParagraphStyle('Resumen', fontSize=14, leading=20, justifyBreaks=1, alignment=TA_LEFT, justifyLastLine=1)
        textinfo = """Generated by SOW project<br/>
        Date: """+str(date)+"""<br/>
        Time: """+str(ts)+"""<br/>
        """
        paragraphReportSummary = Paragraph(text, psDetalle)
        paragraphReportinfo = Paragraph(textinfo, infor)
        self.elements.append(paragraphReportSummary)
        spacer = Spacer(2, 1)
        self.elements.append(spacer)
        self.elements.append(paragraphReportinfo)
        self.elements.append(PageBreak())

    def nextPagesHeader(self, isSecondPage):
        if isSecondPage:
            psHeaderText = ParagraphStyle('Hed0', fontSize=16, alignment=TA_LEFT, borderWidth=3, textColor=self.colorOhkaGreen0)
            text = 'REPORT  SECTIONS'
            paragraphReportHeader = Paragraph(text, psHeaderText)
            self.elements.append(paragraphReportHeader)

            spacer = Spacer(10, 10)
            self.elements.append(spacer)

            d = Drawing(500, 1)
            line = Line(-15, 0, 483, 0)
            line.strokeColor = self.colorOhkaGreenLineas
            line.strokeWidth = 2
            d.add(line)
            self.elements.append(d)

            spacer = Spacer(10, 1)
            self.elements.append(spacer)

            d = Drawing(500, 1)
            line = Line(-15, 0, 483, 0)
            line.strokeColor = self.colorOhkaGreenLineas
            line.strokeWidth = 0.5
            d.add(line)
            self.elements.append(d)

            spacer = Spacer(10, 22)
            self.elements.append(spacer)

    def remoteSessionTableMaker(self):     
        psHeaderText = ParagraphStyle('Hed0', fontSize=18, alignment=TA_LEFT, borderWidth=3, textColor=self.colorOhkaBlue0)
        text = 'Origin System Model--Input'
        paragraphReportHeader = Paragraph(text, psHeaderText)
        self.elements.append(paragraphReportHeader)

        spacer = Spacer(10, 22)
        self.elements.append(spacer)

        psHeaderText = ParagraphStyle('Resumen', fontSize=12, leading=16, justifyBreaks=1, alignment=TA_LEFT, justifyLastLine=1)
        text = 'Dependency model for origin system'
        paragraphReportHeader = Paragraph(text, psHeaderText)
        self.elements.append(paragraphReportHeader)

        spacer = Spacer(10, 22)
        self.elements.append(spacer)

        img = Image('static/org.png')
        img.drawHeight = 2.5*inch
        img.drawWidth = 1.5*inch
        self.elements.append(img)

        spacer = Spacer(10, 22)
        self.elements.append(spacer)
        """
        Create the line items
        """
        d = []
        textData = ["No.", "From", "To", "Type"]
                
        fontSize = 10
        centered = ParagraphStyle(name="centered", alignment=TA_CENTER)
        for text in textData:
            ptext = "<font size='%s'><b>%s</b></font>" % (fontSize, text)
            titlesTable = Paragraph(ptext, centered)
            d.append(titlesTable)        

        data = [d]
        lineNum = 1
        formattedLineData = []

        alignStyle = [ParagraphStyle(name="01", alignment=TA_CENTER),
                      ParagraphStyle(name="02", alignment=TA_CENTER),
                      ParagraphStyle(name="03", alignment=TA_CENTER),
                      ParagraphStyle(name="04", alignment=TA_CENTER)]
        dx=self.data_obj["Dep"]
        lineno=1
        for row in range(len(dx)):
            item=dx[row]
            if("<-->" in item):
                x=item.split("<-->")
                op="<-->"
                lineData = [str(lineno), x[0], x[1], "dependency"]
                #data.append(lineData)
                columnNumber = 0
                for item in lineData:
                    ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
                    p = Paragraph(ptext, alignStyle[columnNumber])
                    formattedLineData.append(p)
                    columnNumber = columnNumber + 1
                data.append(formattedLineData)

                lineno+=1
                formattedLineData = []
                lineData = [str(lineno), x[1], x[0], "dependency"]
                #data.append(lineData)
                columnNumber = 0
                for item in lineData:
                    ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
                    p = Paragraph(ptext, alignStyle[columnNumber])
                    formattedLineData.append(p)
                    columnNumber = columnNumber + 1
                data.append(formattedLineData)
                lineno+=1


            elif("-->" in item):    
                x=item.split("-->")
                op="-->" 
                
                lineData = [str(lineno), x[0], x[1], "dependency"]
                #data.append(lineData)
                columnNumber = 0
                for item in lineData:
                    ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
                    p = Paragraph(ptext, alignStyle[columnNumber])
                    formattedLineData.append(p)
                    columnNumber = columnNumber + 1
                data.append(formattedLineData)
                lineno+=1

            elif("<--" in item):
                x=item.split("<--")
                op="<--"
                lineData = [str(lineno), x[0], x[1], "dependency"]
                #data.append(lineData)
                columnNumber = 0
                for item in lineData:
                    ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
                    p = Paragraph(ptext, alignStyle[columnNumber])
                    formattedLineData.append(p)
                    columnNumber = columnNumber + 1
                data.append(formattedLineData)

                lineno+=1

            else:
                print("unknown operator!!")

            formattedLineData = []
            
        # Row for total
        formattedLineData = []
        totalRow = ["Data Num: "+str(len(self.data_obj["Data"])),"","","ServiceNum: "+str(len(self.data_obj["Server"]))]
        # totalRow.extend(self.data_obj[0])
        # totalRow = ["Data Type:"].extend(self.data_obj[0])
        columnNumber = 0
        for item in totalRow:
            ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
            p = Paragraph(ptext, ParagraphStyle(name="01", alignment=TA_LEFT))
            columnNumber = columnNumber + 1
            formattedLineData.append(p)
        data.append(formattedLineData)

        # formattedLineData = []
        # totalRow = ["Service:",str(len(self.data_obj[1])),"",""]
        # # totalRow.extend(self.data_obj[1])
        # # totalRow = ["Data Type:"].extend(self.data_obj[0])
        # for item in totalRow:
        #     ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
        #     p = Paragraph(ptext, alignStyle[1])
        #     formattedLineData.append(p)
        # data.append(formattedLineData)
        
        #print(data)
        table = Table(data, colWidths=[50, 80, 80, 80])
        tStyle = TableStyle([ #('GRID',(0, 0), (-1, -1), 0.5, grey),
                ('ALIGN', (0, 0), (0, -1), 'LEFT'),
                #('VALIGN', (0, 0), (-1, -1), 'TOP'),
                ("ALIGN", (1, 0), (1, -1), 'RIGHT'),
                ('LINEABOVE', (0, 0), (-1, -1), 1, self.colorOhkaBlue1),
                ('BACKGROUND',(0, 0), (-1, 0), self.colorOhkaGreenLineas),
                ('BACKGROUND',(0, -1),(-1, -1), self.colorOhkaBlue1),
                ('SPAN',(0,-1),(-2,-1))
                ])
        table.setStyle(tStyle)
        self.elements.append(table)

    def xxSessionTableMaker(self):
        self.elements.append(PageBreak())   
        psHeaderText = ParagraphStyle('Hed0', fontSize=18, alignment=TA_LEFT, borderWidth=3, textColor=self.colorOhkaBlue0)
        text = 'System Model After Dependency Analysis--Output'
        paragraphReportHeader = Paragraph(text, psHeaderText)
        self.elements.append(paragraphReportHeader)

        spacer = Spacer(10, 22)
        self.elements.append(spacer)

        psHeaderText = ParagraphStyle('Resumen', fontSize=12, leading=16, justifyBreaks=1, alignment=TA_LEFT, justifyLastLine=1)
        text = 'Dependency analysis'
        paragraphReportHeader = Paragraph(text, psHeaderText)
        self.elements.append(paragraphReportHeader)

        spacer = Spacer(10, 22)
        self.elements.append(spacer)

        img = Image('static/rel.png')
        img.drawHeight = 2.5*inch
        img.drawWidth = 2.5*inch
        self.elements.append(img)

        spacer = Spacer(10, 22)
        self.elements.append(spacer)
        """
        Create the line items
        """
        d = []
        textData = ["No.", "From", "To", "Type"]
                
        fontSize = 10
        centered = ParagraphStyle(name="centered", alignment=TA_CENTER)
        for text in textData:
            ptext = "<font size='%s'><b>%s</b></font>" % (fontSize, text)
            titlesTable = Paragraph(ptext, centered)
            d.append(titlesTable)        

        data = [d]
        lineNum = 1
        formattedLineData = []

        alignStyle = [ParagraphStyle(name="01", alignment=TA_CENTER),
                      ParagraphStyle(name="02", alignment=TA_CENTER),
                      ParagraphStyle(name="03", alignment=TA_CENTER),
                      ParagraphStyle(name="04", alignment=TA_CENTER)]
        
        dx=self.graph#self.data_obj[2]
        lineno=1
        for i in range(len(self.dataitems)):
          for j in range(len(self.dataitems)):
            # item=dx[row]
            # if("<-->" in item):
            #     x=item.split("<-->")
            #     op="<-->"
            if self.graph[i][j][0]==1:
                lineData = [str(lineno), self.dataitems[i], self.dataitems[j], "dependency"]
                #data.append(lineData)
                columnNumber = 0
                for item in lineData:
                    ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
                    p = Paragraph(ptext, alignStyle[columnNumber])
                    formattedLineData.append(p)
                    columnNumber = columnNumber + 1
                data.append(formattedLineData)

                lineno+=1
                # formattedLineData = []
                # lineData = [str(lineno), x[1], x[0], "dependency"]
                # #data.append(lineData)
                # columnNumber = 0
                # for item in lineData:
                #     ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
                #     p = Paragraph(ptext, alignStyle[columnNumber])
                #     formattedLineData.append(p)
                #     columnNumber = columnNumber + 1
                # data.append(formattedLineData)
                # lineno+=1


            
                formattedLineData = []
            
        # Row for total
        formattedLineData = []
        totalRow = ["Data Num: 0","","","ServiceNum: "+str(len(self.data_obj["Server"]))]
        # totalRow.extend(self.data_obj[0])
        # totalRow = ["Data Type:"].extend(self.data_obj[0])
        columnNumber = 0
        for item in totalRow:
            ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
            p = Paragraph(ptext, ParagraphStyle(name="01", alignment=TA_LEFT))
            columnNumber = columnNumber + 1
            formattedLineData.append(p)
        data.append(formattedLineData)

        # formattedLineData = []
        # totalRow = ["Service:",str(len(self.data_obj[1])),"",""]
        # # totalRow.extend(self.data_obj[1])
        # # totalRow = ["Data Type:"].extend(self.data_obj[0])
        # for item in totalRow:
        #     ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
        #     p = Paragraph(ptext, alignStyle[1])
        #     formattedLineData.append(p)
        # data.append(formattedLineData)
        
        #print(data)
        table = Table(data, colWidths=[50, 80, 80, 80])
        tStyle = TableStyle([ #('GRID',(0, 0), (-1, -1), 0.5, grey),
                ('ALIGN', (0, 0), (0, -1), 'LEFT'),
                #('VALIGN', (0, 0), (-1, -1), 'TOP'),
                ("ALIGN", (1, 0), (1, -1), 'RIGHT'),
                ('LINEABOVE', (0, 0), (-1, -1), 1, self.colorOhkaBlue1),
                ('BACKGROUND',(0, 0), (-1, 0), self.colorOhkaGreenLineas),
                ('BACKGROUND',(0, -1),(-1, -1), self.colorOhkaBlue1),
                ('SPAN',(0,-1),(-2,-1))
                ])
        table.setStyle(tStyle)
        self.elements.append(table)

    def inSiteSessionTableMaker(self):
        self.elements.append(PageBreak())
        # psHeaderText = ParagraphStyle('Hed0', fontSize=12, alignment=TA_LEFT, borderWidth=3, textColor=self.colorOhkaBlue0)
        psHeaderText = ParagraphStyle('Hed0', fontSize=18, alignment=TA_LEFT, borderWidth=3, textColor=self.colorOhkaBlue0)
        text = 'Mixed System Model--Output'
        paragraphReportHeader = Paragraph(text, psHeaderText)
        self.elements.append(paragraphReportHeader)

        spacer = Spacer(10, 22)
        self.elements.append(spacer)

        psHeaderText = ParagraphStyle('Resumen', fontSize=12, leading=16, justifyBreaks=1, alignment=TA_LEFT, justifyLastLine=1)
        text = 'Degradation model for origin system'
        paragraphReportHeader = Paragraph(text, psHeaderText)
        self.elements.append(paragraphReportHeader)
        spacer = Spacer(10, 22)
        self.elements.append(spacer)

        img = Image('static/degra.png')
        img.drawHeight = 2.5*inch
        img.drawWidth = 1.5*inch
        self.elements.append(img)

        spacer = Spacer(10, 22)
        self.elements.append(spacer)


        """
        Create the line items
        """
        d = []
        textData = ["No.", "From", "To", "Type"]
                
        fontSize = 8
        centered = ParagraphStyle(name="centered", alignment=TA_CENTER)
        for text in textData:
            ptext = "<font size='%s'><b>%s</b></font>" % (fontSize, text)
            titlesTable = Paragraph(ptext, centered)
            d.append(titlesTable)        

        data = [d]
        lineNum = 1
        formattedLineData = []

        alignStyle = [ParagraphStyle(name="01", alignment=TA_CENTER),
                      ParagraphStyle(name="02", alignment=TA_CENTER),
                      ParagraphStyle(name="03", alignment=TA_CENTER),
                      ParagraphStyle(name="04", alignment=TA_CENTER)]

        # dx=self.graph_degra#self.data_obj[2]
        lineno=1
        for i in range(len(self.dataitems)):
          for j in range(len(self.dataitems)):
            # item=dx[row]
            # if("<-->" in item):
            #     x=item.split("<-->")
            #     op="<-->"
            if self.graph_degra[i][j][0]==1:
                type="Dependency"
                if  self.graph_degra[i][j][1]==1:
                  type="Degradation"
                lineData = [str(lineno), self.dataitems[i], self.dataitems[j],type ]
                #data.append(lineData)
                columnNumber = 0
                for item in lineData:
                    ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
                    p = Paragraph(ptext, alignStyle[columnNumber])
                    formattedLineData.append(p)
                    columnNumber = columnNumber + 1
                data.append(formattedLineData)

                lineno+=1
                # formattedLineData = []
                # lineData = [str(lineno), x[1], x[0], "dependency"]
                # #data.append(lineData)
                # columnNumber = 0
                # for item in lineData:
                #     ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
                #     p = Paragraph(ptext, alignStyle[columnNumber])
                #     formattedLineData.append(p)
                #     columnNumber = columnNumber + 1
                # data.append(formattedLineData)
                # lineno+=1


            
                formattedLineData = []
            
        # Row for total
        formattedLineData = []
        totalRow = ["Data Num: 0","","","ServiceNum: "+str(len(self.data_obj["Server"]))]
        # totalRow.extend(self.data_obj[0])
        # totalRow = ["Data Type:"].extend(self.data_obj[0])
        columnNumber = 0
        for item in totalRow:
            ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
            p = Paragraph(ptext, ParagraphStyle(name="01", alignment=TA_LEFT))
            columnNumber = columnNumber + 1
            formattedLineData.append(p)
        data.append(formattedLineData)
        
        #print(data)
        table = Table(data, colWidths=[50, 80, 80, 80, 80])
        tStyle = TableStyle([ #('GRID',(0, 0), (-1, -1), 0.5, grey),
                ('ALIGN', (0, 0), (0, -1), 'LEFT'),
                #('VALIGN', (0, 0), (-1, -1), 'TOP'),
                ("ALIGN", (1, 0), (1, -1), 'RIGHT'),
                ('LINEABOVE', (0, 0), (-1, -1), 1, self.colorOhkaBlue1),
                ('BACKGROUND',(0, 0), (-1, 0), self.colorOhkaGreenLineas),
                ('BACKGROUND',(0, -1),(-1, -1), self.colorOhkaBlue1),
                ('SPAN',(0,-1),(-2,-1))
                ])
        table.setStyle(tStyle)
        self.elements.append(table)

    # def extraActivitiesTableMaker(self):
    #     self.elements.append(PageBreak())
    #     # psHeaderText = ParagraphStyle('Hed0', fontSize=12, alignment=TA_LEFT, borderWidth=3, textColor=self.colorOhkaBlue0)
    #     psHeaderText = ParagraphStyle('Hed0', fontSize=18, alignment=TA_LEFT, borderWidth=3, textColor=self.colorOhkaBlue0)
    #     text = 'The Result of Hierarchy Analysis--Output'
    #     paragraphReportHeader = Paragraph(text, psHeaderText)
    #     self.elements.append(paragraphReportHeader)

    #     spacer = Spacer(10, 22)
    #     self.elements.append(spacer)

    #     psHeaderText = ParagraphStyle('Resumen', fontSize=12, leading=16, justifyBreaks=1, alignment=TA_LEFT, justifyLastLine=1)
    #     text = 'Hierarchy Analysis'
    #     paragraphReportHeader = Paragraph(text, psHeaderText)
    #     self.elements.append(paragraphReportHeader)
    #     spacer = Spacer(10, 22)
    #     self.elements.append(spacer)

    #     # psHeaderText = ParagraphStyle('Hed0', fontSize=12, alignment=TA_LEFT, borderWidth=3, textColor=self.colorOhkaBlue0)
    #     # text = 'Hierarchy Analysis'
    #     # paragraphReportHeader = Paragraph(text, psHeaderText)
    #     # self.elements.append(paragraphReportHeader)

    #     spacer = Spacer(10, 22)
    #     self.elements.append(spacer)
    #     """
    #     Create the line items
    #     """
    #     prevLevel=None
    #     L=[]
    #     tmpL=[]
    #     for v,k in sorted(levels):
    #       if prevLevel!=v:
    #         if prevLevel!=None:
    #           print ("")
    #         if len(tmpL)>0:
    #           L.append(tmpL)
    #         tmpL=[]
            
    #       prevLevel=v
    #       tmpL.append(k)
    #       print (str(k)+",",end="")
    #       print("")
    #     L.append(tmpL)

    #     d = []
    #     textData = ["Level", "Services"]
                
    #     fontSize = 8
    #     centered = ParagraphStyle(name="centered", alignment=TA_CENTER)
    #     for text in textData:
    #         ptext = "<font size='%s'><b>%s</b></font>" % (fontSize, text)
    #         titlesTable = Paragraph(ptext, centered)
    #         d.append(titlesTable)        

    #     data = [d]
    #     lineNum = 1
    #     formattedLineData = []

    #     alignStyle = [ParagraphStyle(name="01", alignment=TA_CENTER),
    #                   ParagraphStyle(name="02", alignment=TA_CENTER)]

    #     for row in range(len(L)):
    #         items=""
    #         flag=True
    #         for item in L[row]:
    #           if flag:
    #             flag=False
    #           else:
    #             items+=", "
    #           items+=str(self.dataitems[item])
              
    #         lineData = [str(lineNum),items]
    #         #data.append(lineData)
    #         columnNumber = 0
    #         for item in lineData:
    #             ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
    #             p = Paragraph(ptext, alignStyle[columnNumber])
    #             formattedLineData.append(p)
    #             columnNumber = columnNumber + 1
    #         data.append(formattedLineData)
    #         formattedLineData = []
    #         lineNum+=1
            
    #     # Row for total
    #     # totalRow = ["Total de Horas", "", "", "", "30:15"]
    #     # for item in totalRow:
    #     #     ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
    #     #     p = Paragraph(ptext, alignStyle[1])
    #     #     formattedLineData.append(p)
    #     # data.append(formattedLineData)
        
    #     #print(data)
    #     table = Table(data, colWidths=[50, 200])
    #     tStyle = TableStyle([ ('GRID',(0, 0), (-1, -1), 0.5, self.colorOhkaBlue1),
    #             ('ALIGN', (0, 0), (0, -1), 'LEFT'),
    #             #('VALIGN', (0, 0), (-1, -1), 'TOP'),
    #             ("ALIGN", (1, 0), (1, -1), 'RIGHT'),
    #             ('LINEABOVE', (0, 0), (-1, -1), 1, self.colorOhkaBlue1),
    #             # ('LINEBOTTOM', (0, 0), (-1, -1), 1, self.colorOhkaBlue1),
    #             ('BACKGROUND',(0, 0), (-1, 0), self.colorOhkaGreenLineas),
    #             # ('BACKGROUND',(0, -1),(-1, -1), self.colorOhkaBlue1),
    #             ('SPAN',(0,-1),(-2,-1))
    #             ])
    #     table.setStyle(tStyle)
    #     self.elements.append(table)

    def summaryTableMaker(self):
        self.elements.append(PageBreak())
        # psHeaderText = ParagraphStyle('Hed0', fontSize=12, alignment=TA_LEFT, borderWidth=3, textColor=self.colorOhkaBlue0)
        psHeaderText = ParagraphStyle('Hed0', fontSize=18, alignment=TA_LEFT, borderWidth=3, textColor=self.colorOhkaBlue0)
        text = 'The Result of Model Consistency Analysis--Output'
        paragraphReportHeader = Paragraph(text, psHeaderText)
        self.elements.append(paragraphReportHeader)

        spacer = Spacer(10, 22)
        self.elements.append(spacer)

        psHeaderText = ParagraphStyle('Resumen', fontSize=12, leading=16, justifyBreaks=1, alignment=TA_LEFT, justifyLastLine=1)
        text = 'Model Consistency Analysis'
        paragraphReportHeader = Paragraph(text, psHeaderText)
        self.elements.append(paragraphReportHeader)
        spacer = Spacer(10, 22)
        self.elements.append(spacer)
        # psHeaderText = ParagraphStyle('Hed0', fontSize=12, alignment=TA_LEFT, borderWidth=3, textColor=self.colorOhkaBlue0)
        # text = 'REGISTRO TOTAL DE HORAS'
        # paragraphReportHeader = Paragraph(text, psHeaderText)
        # self.elements.append(paragraphReportHeader)

        spacer = Spacer(10, 22)
        self.elements.append(spacer)

        img = Image('static/degra.png')
        img.drawHeight = 2.5*inch
        img.drawWidth = 1.5*inch
        self.elements.append(img)

        spacer = Spacer(10, 22)
        self.elements.append(spacer)

        """
        Create the line items
        """

        tStyle = TableStyle([
                   ('ALIGN', (0, 0), (0, -1), 'LEFT'),
                   #('VALIGN', (0, 0), (-1, -1), 'TOP'),
                   ("ALIGN", (1, 0), (1, -1), 'RIGHT'),
                   ('LINEABOVE', (0, 0), (-1, -1), 1, self.colorOhkaBlue1),
                   ('BACKGROUND',(-2, -1),(-1, -1), self.colorOhkaGreen2)
                   ])

        fontSize = 8

        lineData=[]
        lineNum=1
        for cycle in self.cycles:
            cycle_str=""
            flag=True
            for item in cycle:
                if flag:
                    flag=False
                else:
                    cycle_str+=" -> "
            
                cycle_str+=item
            lineData.append([lineNum,cycle_str])

        

        # lineData = [["0",cycle_str ]]
            #         for item in lineData:
    #             ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
    #             p = Paragraph(ptext, alignStyle[columnNumber])
    #             formattedLineData.append(p)
    #             columnNumber = columnNumber + 1
    #         data.append(formattedLineData)
        alignStyle = [ParagraphStyle(name="01", alignment=TA_CENTER),
                      ParagraphStyle(name="02", alignment=TA_CENTER),
                      ParagraphStyle(name="03", alignment=TA_CENTER),
                      ParagraphStyle(name="04", alignment=TA_CENTER)]

        data=[]
        formattedLineData=[]
        for row in lineData:
            for item in row:
                ptext = "<font size='%s'>%s</font>" % (fontSize-1, item)
                p = Paragraph(ptext, ParagraphStyle(name="01", alignment=TA_CENTER))
                formattedLineData.append(p)
            data.append(formattedLineData)
            formattedLineData = []

        table = Table(data, colWidths=[50, 200])
        table.setStyle(tStyle)
        self.elements.append(table)

        # Total de horas contradas vs horas consumidas
        # data = []
        # formattedLineData = []

        # lineData = [["Total de horas contratadas", "120:00"],
        #             ["Horas restantes por consumir", "00:00"]]

        # for row in lineData:
        #     for item in row:
        #         ptext = "<b>{}</b>".format(item)
        #         p = Paragraph(ptext, self.styleSheet["BodyText"])
        #         formattedLineData.append(p)
        #     data.append(formattedLineData)
        #     formattedLineData = []

        # table = Table(lineData, colWidths=[400, 100])
        # tStyle = TableStyle([
        #         ('ALIGN', (0, 0), (0, -1), 'LEFT'),
        #         ("ALIGN", (1, 0), (1, -1), 'RIGHT'),
        #         ('BACKGROUND', (0, 0), (1, 0), self.colorOhkaBlue1),
        #         ('BACKGROUND', (0, 1), (1, 1), self.colorOhkaGreen1),
        #         ])
        # table.setStyle(tStyle)

        # spacer = Spacer(10, 150)
        # self.elements.append(spacer)
        # self.elements.append(table)
